import { hasToken } from '@/utils/token'
import React, { ComponentType } from 'react'
import { Redirect, Route, RouteProps } from 'react-router-dom'

// RouteProps : Route组件的props类型
// ComponentType : react内置的组件类型

export default function AuthRoute({ component, ...rest }: RouteProps) {
  // rest = { path: '/xxx', exact: true, a: 'aa', b: 'bb' }
  // console.log(exact)
  const Component = component as ComponentType
  return (
    <Route
      {...rest}
      render={(props) => {
        // 路由信息对象 { history: xxx, location: xxx, match: xxxx, ....}
        console.log(props)
        if (!hasToken())
          return (
            <Redirect
              to={{
                pathname: '/login',
                state: {
                  from: props.location.pathname, // 标记将来登录成功以后需要回跳到哪个页面
                },
              }}
            />
          )
        return <Component />
      }}
    ></Route>
  )
}
